<?php

class MobileController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
        $this->_helper->layout()->disableLayout();
    }

    public function indexAction()
    {
        // action body
    }

    public function takeitemAction()
    {
        $tcode = $this->getRequest()->getParam("tcode");
        
        // Check parameter
        if($tcode == null){
            $this->setMessage(false,"Data tidak lengkap");
            return;
        }
        
        // Check code
        $trans_db = new Application_Model_DbTable_Transaction();
        $id = $trans_db->getId($tcode);
        if($id == null){
            $this->setMessage(false,"Kode tidak ditemukan");
            return;
        }
        
        // Check status id, it must be 'Sudah disetujui' (3)
        if($trans_db->getStatus($id) != 3){
            $this->setMessage(false,"Status transaksi tidak valid, barang tidak dapat diambil");
            return;
        }
        
        // Update transaction status, delivery/pick up time and last update
        $trans_db->updateStatus($id, 4);
        $time = date("Y/m/d H:i:s");
        $trans_db->setDeliveryTime($id,$time);
        $trans_db->lastUpdate($id, $time);
        
        $this->setMessage(true,"Status ".strtoupper($tcode)." diubah menjadi 'Sedang Diantar'");
    }

    public function receiveitemAction()
    {
        $tcode = $this->getRequest()->getParam("tcode");
		$puk = $this->getRequest()->getParam("puk");
		$name = $this->getRequest()->getParam("name");
		$ic = $this->getRequest()->getParam("ic");
		$relation = $this->getRequest()->getParam("relation");
		
		// Check parameter
        if($tcode == null || $puk == null){
            $this->setMessage(false,"Data tidak lengkap");
            return;
        }
		
		// Check code
        $trans_db = new Application_Model_DbTable_Transaction();
        $id = $trans_db->getId($tcode);
        if($id == null){
            $this->setMessage(false,"Kode tidak ditemukan");
            return;
        }

        $trans = $trans_db->getTransaction($id);
		// Check status id, it must be 'Sedang diantar' (4)
        if($trans["transaction_status_id"] != 4){
            $this->setMessage(false,"Status transaksi tidak valid, barang tidak dapat diserahkan");
            return;
        }
		
		// Check puk
		$puk_db = $trans_db->getPUK($id);
		if($puk != $puk_db){
			$this->setMessage(false,"PUK tidak cocok");
            return;
		}
		
		// Add receiver, update transaction status and last update/received time
		$trans_db->addReceiver($id,$name,$ic,$relation);
        $trans_db->updateStatus($id, 5);
        $trans_db->lastUpdate($id, date("Y/m/d H:i:s"));

        // Create row for testimony
        $testimony_db = new Application_Model_DbTable_Testimony();
        $testimony_db->addTestimony($trans["transaction_id"],$trans["shop_id"],$trans["buyer_id"]);
        
        $this->setMessage(true,"Status ".strtoupper($tcode)." diubah menjadi 'Sudah Diterima'");
    }

    public function loginAction()
    {
        $username = $this->getRequest()->getParam("username");
        $password = $this->getRequest()->getParam("password");
        
        // Check parameter
        if($username == null){
            $this->setMessage(false,"Data tidak lengkap");
            return;
        }
        
        // Check username
        $admin_db = new Application_Model_DbTable_Admin();
        $admin = $admin_db->getAdminId($username);
        if($admin == null){
            $this->setMessage(false,"Username tidak ditemukan");
            return;
        }
        
        // Check role, it must be 'Kurir' (4)
        if($admin["role_id"] != 4){
            $this->setMessage(false,"Hanya kurir yang bisa mengakses sistem ini");
            return;
        }
        
        // Check password
        if(!$admin_db->checkLogin($username, $password)){
            $this->setMessage(false,"Password salah");
            return;
        }
        
        $this->setMessage(true,"Login berhasil");
    }

    public function setMessage($success, $message){
        $this->view->success = $success;
        $this->view->message = $message;
    }
}







